package com.itb.copter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.itb.copter.constan.Constans;
import com.itb.copter.function.Json;
 
public class MainActivity extends Activity implements OnClickListener{
    Button button;
    TextView outputText,outputLastName,outputTextEng,outputLastNameEng,outputBirthDay;
    ImageView imageView,imgSex;
    ProgressBar progressBar;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        button = (Button) findViewById(R.id.button);
        outputText = (TextView) findViewById(R.id.outputTxtName);
        outputLastName = (TextView) findViewById(R.id.outputTxtLastName);
        outputTextEng = (TextView) findViewById(R.id.outputNameEng);
        outputLastNameEng = (TextView) findViewById(R.id.outputLaseNameEng);
        outputBirthDay = (TextView) findViewById(R.id.outputBirthDay);
        imageView = (ImageView) findViewById(R.id.imageView1);
        imgSex = (ImageView) findViewById(R.id.img_sex);
        progressBar = (ProgressBar)findViewById(R.id.progressBar);
        button.setOnClickListener(this);
        progressBar.setVisibility(View.GONE);
    }
 
   
 
    public void onClick(View view) {
        GetXMLTask task = new GetXMLTask();
        button.setEnabled(false);
        task.execute(new String[] { Constans.URL_SERVER });
        progressBar.setVisibility(View.VISIBLE);
    }
 
    private class GetXMLTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String output = null;
            for (String url : urls) {
                output = getOutputFromUrl(url);
            }
            return output;
        }
 
        private String getOutputFromUrl(String url) {
            String output = null;
            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        		urlParameters.add(new BasicNameValuePair("ID", "52280318"));
         
//        		httpGet.setEntity(new UrlEncodedFormEntity(urlParameters));
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                output = EntityUtils.toString(httpEntity);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e){
                e.printStackTrace();
            }
            return output;
        }
 
        @Override
        protected void onPostExecute(String jsonString) {
        	outputText.setText(Json.getValue(jsonString, Constans.PRE_NAME_THAI)+Json.getValue(jsonString, Constans.FIRST_NAME_THAI));
        	outputLastName.setText(Json.getValue(jsonString, Constans.LASE_NAME_THAI));
        	
        	outputTextEng.setText(Json.getValue(jsonString, Constans.PRE_NAME_ENG)+Json.getValue(jsonString, Constans.FIRST_NAME_ENG));
        	outputLastNameEng.setText(Json.getValue(jsonString, Constans.LASE_NAME_ENG));
        	
        	outputBirthDay.setText(Json.getValue(jsonString, Constans.BIRTH_DATE_STR));
        	if(Json.getValue(jsonString, Constans.SEX).equals("1")){
        		imgSex.setImageResource(R.drawable.man);
        	}else{
        		imgSex.setImageResource(R.drawable.woman);
        	}
        	
        	String imgDate[] = Json.getValue(jsonString, Constans.IMG_DATA).split(",");
        	byte[] decodedString = Base64.decode(imgDate[1].trim(), Base64.DEFAULT);
        	Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
        	imageView.setImageBitmap(decodedByte);
        	button.setEnabled(true);
        	progressBar.setVisibility(View.GONE);
        }
    }
}
